#!/usr/bin/env python
#-*- coding:utf-8 -*-
import ConfigParser
import MySQLdb
import getpass
import hashlib
import re
import os
import subprocess

print('=' * 50)
print('安装脚本'.center(54))
print('=' * 50)

print

config = ConfigParser.ConfigParser()
user = raw_input('请输入连接数据库的用户名: ')
passwd = getpass.getpass('请输入连接数据库的密码: ')
host = raw_input('请输入连接数据库的IP: ')
port = int(raw_input('请输入连接数据库的端口号: '))

with MySQLdb.connect(user=user, passwd=passwd, host=host, port=port) as c:
	print('连接成功!')

	print

	database = raw_input('请输入需要创建的数据库名: ')
	c.execute("drop database if exists `%s`" % database) 
	c.execute('create database `%s`' % database)

	config.add_section('mysql')
	config.set('mysql', 'user', user)
	config.set('mysql', 'password', passwd)
	config.set('mysql', 'host', host)
	config.set('mysql', 'port', port)
	config.set('mysql', 'database', database)

	subprocess.call("mysql --user=%s --password=%s --database=%s --host=%s --port=%s < schema.sql" %
		(user, passwd, database, host, port), shell=True)

	print('数据库初始化成功!')

	print


	with open('pic-share.cfg', 'w') as cfg:
		config.write(cfg)

	print('配置文件生成成功!')

	print('创建头像存放目录!')

	os.system("sudo mkdir ./tmp ; sudo chmod a+rwx ./tmp -R")

	print

	raw_input('Press any key to continue...')
